import { MapOptions, MarkerOptions } from './interfaces/Options';
import { PreviousMarker } from './interfaces/PreviousMarker';
import { setGeoCtrl } from './controls/GeoControl';
import { setScaleCtrl } from './controls/ScaleControl';
import { setOverviewMapCtrl } from './controls/OverviewMapControl';
import { setNavigationCtrl } from './controls/NavigationControl';

export const reCenter = function (map: any, opts: MapOptions) {
    let BMap: any = (<any>window)['BMap'];
    if (opts.center) {
        map.setCenter(new BMap.Point(opts.center.longitude, opts.center.latitude));
    }
};

export const reZoom = function (map: any, opts: MapOptions) {
    if (opts.zoom) {
        map.setZoom(opts.zoom);
    }
};

export const createInstance = function (opts: MapOptions, element: any) {
    let BMap: any = (<any>window)['BMap'];
    // create map instance
    let map = new BMap.Map(element);
    // init map, set central location and zoom level
    map.centerAndZoom(new BMap.Point(opts.center.longitude, opts.center.latitude), opts.zoom);

    setNavigationCtrl(map, opts);
    setScaleCtrl(map, opts);
    setOverviewMapCtrl(map, opts);
    if (opts.enableScrollWheelZoom) {
        //enable scroll wheel zoom
        map.enableScrollWheelZoom();
    }
    setGeoCtrl(map, opts);
    return map;
};

export const createMarker = function (marker: MarkerOptions, pt: any) {
    let BMap: any = (<any>window)['BMap'];
    let opts: any = {};
    if (marker.icon) {
        let icon = new BMap.Icon(marker.icon, new BMap.Size(marker.width, marker.height));
        opts['icon'] = icon;
    }
    if (marker.enableDragging) {
        opts['enableDragging'] = true;
    }
    return new BMap.Marker(pt, opts);
};

export const redrawMarkers = function (map: any, previousMarkers: PreviousMarker[], opts: MapOptions) {
    let BMap: any = (<any>window)['BMap'];
    let self = this;

    previousMarkers.forEach(function ({ marker, listeners }) {
        listeners.forEach(listener => { marker.removeEventListener('click', listener); });
        map.removeOverlay(marker);
    });

    previousMarkers.length = 0;

    if (!opts.markers) {
        return;
    }

    opts.markers.forEach(function (marker: MarkerOptions) {
        let marker2 = createMarker(marker, new BMap.Point(marker.longitude, marker.latitude));
        marker2.devname = marker.devname;
        // add marker to the map
        map.addOverlay(marker2);
        let previousMarker: PreviousMarker = { marker: marker2, status: marker.status, listeners: [] };
        previousMarkers.push(previousMarker);


        let onMarkerClickedListener = () => {
            self.onMarkerClicked.emit(marker2);
        };
        marker2.addEventListener('click', onMarkerClickedListener);
        previousMarker.listeners.push(onMarkerClickedListener);
        
        // if (!marker.title && !marker.content) {
        //     return;
        // }
        // let msg = `<p>${marker.title || ''}</p><p>${marker.content || ''}</p>`;
        // let infoWindow2 = new BMap.InfoWindow(msg, {
        //     enableMessage: !!marker.enableMessage,
        //     width: 200,     // 信息窗口宽度    
        //     height: 150,
        // });
        // if (marker.autoDisplayInfoWindow) {
        //     marker2.openInfoWindow(infoWindow2);
        // }
        // let openInfoWindowListener = function () {
        //     this.openInfoWindow(infoWindow2);
        // };
        // previousMarker.listeners.push(openInfoWindowListener);
        // marker2.addEventListener('click', openInfoWindowListener);
    });
};

// 可选择显示地图上的坐标点
export const displayOverlay = function (map: any, previousMarkers: PreviousMarker[], select: number) {
    const BMap = window['BMap'];
    previousMarkers.forEach(function ({ marker, status }) {
        if (select === -1) {
            marker.show();
        } else {
            if (status === select) {
                marker.show();
            } else {
                marker.hide();
            }
        }
    });
};

// 删除所有连线
export const clearPolyline = function (map: any, previousPolylines: PreviousMarker[]) {
    const BMap = window['BMap'];
    previousPolylines.forEach(function (link) {
        map.removeOverlay(link);
    });
};

// 创建连线
export const displayPolyline = function (map: any, previousPolylines: PreviousMarker[], point: any, linkPonits: any) {
    const BMap = window['BMap'];
    linkPonits.forEach(function (linkPonit) {
        const polyline = new BMap.Polyline([
            new BMap.Point(point.longitude, point.latitude),
            new BMap.Point(linkPonit.longitude, linkPonit.latitude)
        ],
            { strokeColor: 'blue', strokeWeight: 2, strokeOpacity: 0.5 }
        );
        previousPolylines.push(polyline);
        map.addOverlay(polyline);
    });
};

// 点击弹出信息框事件
export const markerInfoShow = function (marker,infos) {
    const BMap = window['BMap'];
    const msg = infos;
    const infoWindow2 = new BMap.InfoWindow(msg, {
        enableMessage: !!marker.enableMessage,
        width: 150,     // 信息窗口宽度
        height: 90,
    });
    marker.openInfoWindow(infoWindow2);
};
